package com.souvc.weixin.test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import com.souvc.weixin.copyArticle.Copy;
import com.souvc.weixin.util.CommonUtil;

import net.sf.json.JSONObject;

public class SendGroupImageMessage {
	
    // 图文素材上传接口地址
    private static String postNewsUrl = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token="+CommonUtil.ACCESS_TOKEN;
    // 群发接口地址
    private static String sendToAllUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+CommonUtil.ACCESS_TOKEN;
    // 预览接口地址
    private static String sendToPreviewUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token="+CommonUtil.ACCESS_TOKEN;
  
    
    //私人微信素材(永久)
     private static String wx = "<div><img src='http://mmbiz.qpic.cn/mmbiz_jpg/wduotRKg3SiagQoocfmuIEG9yCQbbIkD0wXRwPWhDPh1MHcAVOWHg7V4LWDzgCx98wIRt6UPRUcFcO5BgicnHFqA/0?wx_fmt=jpeg'></div>";
    //萌新区块链公众号素材(永久)
     private static String gzh = "<div><img src='http://mmbiz.qpic.cn/mmbiz_jpg/wduotRKg3SiagQoocfmuIEG9yCQbbIkD0xNvHyv1RG1sKY5YJCuozZENgXbvTIfKUNO31VPD3yDa7fj4bwCYHKQ/0?wx_fmt=jpeg'></div>";
    
     /**
      * 上传图文信息
      * @author Gakkiyomi
      * @throws Exception
      */
    public String upload() throws Exception{
    	//今日行情
    	String Hangqing = Copy.copyTowx("http://www.bishijie.com/kuaixun_102868")+gzh;
    	//今日快讯
    	String Kuaixun = Copy.copyTowx("http://www.bishijie.com/kuaixun_102872")+gzh+wx;
    	
	     List<String> ls = Copy.copyArticle("http://www.bishijie.com/shendu_4818");
    	//文章标题
    	String title = ls.get(0);
    	//文章正文
    	String content = ls.get(1)+gzh+wx;
    	//行情封面
	 JSONObject cover = CommonUtil.UploadMeida("image","D:/gakki.jpg");
	 System.out.println(cover);
	 String tmediaid = cover.getString("media_id");
	   //快讯封面
	 JSONObject cover2 = CommonUtil.UploadMeida("image","D:/kuaixun.jpg");
	 System.out.println(cover2);
	 String tmediaid2 = cover2.getString("media_id");
	  //文章封面
		 JSONObject cover3 = CommonUtil.UploadMeida("image","D:/Geci.jpg");
		 System.out.println(cover3);
		 String tmediaid3 = cover3.getString("media_id");
	 
		 
	//上传的图文消息数据，其中thumb_media_id是文件上传图片上传的id
	String data = "{\"articles\":[{\"author\":\"萌新区块链\",\"content\":\""+Hangqing+"\",\"show_cover_pic\":0,\"thumb_media_id\":\""+tmediaid+"\",\"digest\":\"  \",\"title\":\"今日行情\"},{\"author\":\"萌新区块链\",\"content\":\""+Kuaixun+"\",\"show_cover_pic\":0,\"thumb_media_id\":\""+tmediaid2+"\",\"digest\":\"  \",\"title\":\"今日快讯\"},{\"author\":\"萌新区块链\",\"content\":\""+content+"\",\"show_cover_pic\":0,\"thumb_media_id\":\""+tmediaid3+"\",\"digest\":\"  \",\"title\":\""+title+"\"}]}";
	JSONObject json = CommonUtil.httpsRequest(postNewsUrl, "POST", data);
	String jj =json.toString();
	System.out.println(jj);
	return jj;
}
  
     //群发图文
   public String sendGroupImageMessage() throws Exception{
	   //上传图文
	   String tuwen = new SendGroupImageMessage().upload();
	   JSONObject jb =JSONObject.fromObject(tuwen);
	   String mediaid = jb.getString("media_id");
       String data = "{\"touser\":\"o4NQZ6GBQOeq-teV-0yNnQdHjoqc\",\"mpnews\":{\"media_id\":\""+mediaid+"\"},\"msgtype\":\"mpnews\",\"send_ignore_reprint\":0}\";";
	   JSONObject json = CommonUtil.httpsRequest(sendToPreviewUrl, "POST", data);
	   return json.toString();
	}
   public static void main(String[] args) throws Exception {
	  //System.out.println(CommonUtil.YJ("image","D:/kuaixun.jpg"));
	  //System.out.println(CommonUtil.YJ("image","D:/gongzhonghao.jpg"));
		System.out.println(new SendGroupImageMessage().sendGroupImageMessage());
	  
   }
}